CREATE PROCEDURE coreCmsRankUp
(
	@cid			int
)
AS
BEGIN
	SET NOCOUNT ON;

	/* Declarations */
	DECLARE		@targetrank		int;
	DECLARE		@sourcerank		int;
	DECLARE		@sourceparentid	int;
	
	/* load source information */
	SELECT	@sourcerank		= ISNULL(Rank, -1), 
			@sourceparentid = ISNULL(FK_ParentID,-1) 
	FROM	Content WHERE PK_ID=@cid;
	IF @sourcerank = -1 RETURN;
	
	/* calculate the target rank and ensure in bounds */
	SELECT @targetrank = @sourcerank-1;
	IF @targetrank < 1 RETURN;

	/* Update the records */
	UPDATE Content SET Rank=@sourcerank WHERE Rank=@targetrank AND FK_ParentID=@sourceparentid;
	UPDATE Content SET Rank=@targetrank WHERE PK_ID=@cid;

	SET NOCOUNT OFF;	
END